const g = getApp().globalData
const util = g.util
const MPLogin = require('../../utils/MPLogin.js')

//审核类型
const approveType = {
    nothing: 1,
    ownerApprove: 0,
    managerApprove: 2,
}

//角色类型
const roleType = {
    nothing: 0,
    owner: 1,
    manager: 2,
}

//是否超级管理员
const isSuperManager = {
    no: 0,
    yes: 1,
}

const myApproval = {
    text: `我的审批`,
    iconPath: `/images/icon/plane.png`,
    detailURL: `../myApproval/myApproval`,
}

const myRoom = {
    text: `我的房间`,
    iconPath: `/images/icon/plane.png`,
    detailURL: `../deviceList/deviceList`,
}
const powerManage = {
    text: `权限管理`,
    iconPath: `/images/icon/plane.png`,
    detailURL: `../powerManage/powerManage`,
}
const ownerApprove = {
    text: `申请业主`,
    iconPath: `/images/icon/plane.png`,
    detailURL: `../roleApprove/roleApprove?applyType=1`,
}
const managerApprove = {
    text: `申请管理员`,
    iconPath: `/images/icon/plane.png`,
    detailURL: `../roleApprove/roleApprove?applyType=2`,
}


Page({
    data: {
        userInfo: {},
    },

    onLoad() {
        if (g.userInfo) {
            // 已登录
            util.showLoading()
            this.getUserInfo()
        } else {
            MPLogin.login(res => {
                if (res) {
                    // 已授权
                    util.showLoading()
                    this.getUserInfo()
                } else {
                    wx.redirectTo({
                        url: '../index/index',
                    })

                }
            })

        }
    },

    onPullDownRefresh() {
        wx.stopPullDownRefresh()
        this.getUserInfo()
    },

    //获取用户信息
    getUserInfo() {
        let url = `users/getUserInfo`
        util.httpGet({
            url: url,
            header: { authorization: g.userInfo.token },
            fn: res => {
                util.hideLoading()
                let userInfo = this.processData(res.data.result)
                this.setData({
                    userInfo
                })
            }

        })
    },

    //处理数据
    processData(data) {
        if (!data) return
        data.optionList = []
        if (data.approve_type == approveType.ownerApprove) {
            data.identity = `业主审核中`
        } else if (data.approve_type == approveType.managerApprove) {
            data.identity = `管理员审核中`
        } else if (data.approve_type == approveType.nothing) {
            if (data.is_superuser == isSuperManager.yes) {
                data.identity = `超级管理员`
                data.optionList = [myApproval, myRoom, powerManage]
            } else if (data.is_superuser == isSuperManager.no) {
                if (data.role_type == roleType.manager) {
                    data.identity = `管理员`
                    data.optionList = [myRoom]
                } else if (data.role_type == roleType.owner) {
                    data.identity = `业主`
                    data.optionList = [myRoom]
                } else if (data.role_type == roleType.nothing) {
                    data.identity = ``
                    data.optionList = [ownerApprove, managerApprove]
                }
            }
        }

        return data
    },

    //跳转到详情页
    goDetail(e) {
        let item = e.currentTarget.dataset.item
        let url = item.detailURL
        if (item.text == `我的房间`) {
            wx.switchTab({
                url: url,
            })
        } else {
            wx.navigateTo({
                url: url,
            })

        }
    },
})